<template>
  <div class="edit-form">
    <FormBasicEquipment
      :submit-handle="submitHandle"
      :data-model="deviceBasic.totalForm"
    />
  </div>
</template>

<script>
import data from "./const";
import FormBasicEquipment from "./form1.vue";
import { mapState, mapMutations } from "vuex";
import { deepClone, deepCopy } from "@/utils/index";
import { formatDateToSecond } from "@/filters";

// const  data2=require("./const")
// const data =deepClone(data1)
export default {
  name: "EditBasicEquipment",
  components: { FormBasicEquipment },

  data() {
    // 组装扩展信息

    return {
      data: deepCopy(data), // JSON.parse(JSON.stringify(data)),
      submitHandle: (params) => {
        this.submitForm(params);
      },
    };
  },
  created() {
    this.$store.dispatch("deviceBasic/setTotalForm", this.data.totalForm);
  },
  mounted() {
    // 判断是编辑还是新增
    this.preloadData();
  },
  computed: {
    ...mapState(["deviceBasic"]),
  },
  methods: {
    async preloadData() {
      const dataKey = this.$route.fullPath;
      const id = localStorage.getItem(dataKey);
      if (id) {
        const data = await this.$store.dispatch(
          "deviceBasic/requestDeviceEditInfo",
          id
        );
        console.log(data, "preloadData");
        // 把详情对象分拆为模板格式

        this.data.totalForm[0].formModel = data;
        this.data.totalForm[2].formModel.archives = data.archives;
        this.$store
          .dispatch(this.$store_url_button, {
            add: this.add || "",
            delete: this.delete || "",
            edit: this.edit || "",
            detail: this.detail || "",
            using: this.using || "",
            useless: this.useless || "",
            downloadIdentify: this.downloadIdentify || "",
          })
          .then(async () => {
            const formModel = data.extra
              ? JSON.parse(data.extra)
              : this.data.totalForm[1].formModel;
            await this.getList(formModel);
          });
      } else {
        // 先把常量信息给vuex

        this.$store
          .dispatch(this.$store_url_button, {
            add: this.add || "",
            delete: this.delete || "",
            edit: this.edit || "",
            detail: this.detail || "",
            using: this.using || "",
            useless: this.useless || "",
            downloadIdentify: this.downloadIdentify || "",
          })
          .then(() => {
            this.getList(); // 表格数据
          });
      }
    },
    submitForm(params) {
      // 格式化拓展信息中的时间 - TODO
      // Object.keys(params[1].formModel).forEach((key) => {
      //   const date = new Date(params[1].formModel[key]).getDate();
      //   // 如果值是时间格式，那么date会返回数字
      //   if (date == date) {
      //     params[1].formModel[key] = formatDateToSecond(
      //       params[1].formModel[key]
      //     );
      //   }
      // });

      const data = Object.assign(
        {},
        params[0].formModel,
        { extra: JSON.stringify(params[1].formModel) },
        params[2].formModel
      );

      this.$store
        .dispatch("deviceBasic/saveDeviceInfo", data)
        .then((response) => {
          this.$store.dispatch("tagsView/delVisitedView", {
            path: this.$route.fullPath,
          });
          this.$router.push("/device-basic/index");
        });
    },
    getList(data) {
      this.$store.dispatch("deviceBasic/requestDeviceBroadInfo", data);
    },
  },
};
</script>
<style lang="scss">
.edit-form {
  margin: 10px;
  .edit-now-info {
    background: white;
  }
  .edit-form-history-info {
    background: white;
    margin: 20px 0;
  }
}
</style>
